# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: reporter\reports\affected_items.py
from reporter.reports.basic_template import ReportTemplate
from io import StringIO
import jinja2
from reporter.reports.basic_template import NamedTempFileWithNormalizedWrite


class AffectedItemsReport(ReportTemplate):
    REPORT_NAME = "Affected Items Report"

    def parts(self):
        template_loader = jinja2.FileSystemLoader(searchpath="data/templates/reports/")
        template_env = jinja2.Environment(loader=template_loader)
        template_vars = {}
        template = template_env.get_template("affected_items_template.html")
        common_parts_template_vars = {}
        common_parts_template = template_env.get_template("common_parts_template.html")
        data = StringIO()
        data.write(common_parts_template.render({"template_part": "header"}))
        common_parts_template_vars["template_part"] = "body_start"
        common_parts_template_vars["report_name"] = self.REPORT_NAME
        common_parts_template_vars["date_generated"] = self.get_formatted_nowdate()
        data.write(common_parts_template.render(common_parts_template_vars))
        data.seek(0)
        yield data
        data = StringIO()
        with NamedTempFileWithNormalizedWrite() as (file_affected_items):
            with NamedTempFileWithNormalizedWrite() as (file_scanned_items):
                for source in self.sources:
                    alerts_count = {3: 0, 2: 0, 1: 0, 0: 0}
                    file_affected_items.seek(0)
                    file_affected_items.truncate()
                    file_affected_items.write(
                        template.render({"template_part": "affected_items_title"})
                    )
                    file_scanned_items.seek(0)
                    file_scanned_items.truncate()
                    file_scanned_items.write(
                        template.render({"template_part": "scanned_items_title"})
                    )
                    for location in source.locations():
                        is_vulnerable = False
                        for vulnerability, _, _ in source.vulnerabilities(
                            location=location
                        ):
                            is_vulnerable = True
                            break

                        location["is_vulnerable"] = is_vulnerable
                        common_parts_template_vars["template_part"] = "scanned_item"
                        location["url"] = self.unquote_url(location.get("url") or "")
                        common_parts_template_vars["location"] = location
                        file_scanned_items.write(
                            common_parts_template.render(common_parts_template_vars)
                        )

                    for vulnerability_type, cnt in source.vulnerability_types():
                        alerts_count[vulnerability_type.get("severity")] += cnt
                        for vulnerability, location, _ in source.vulnerabilities(
                            vtype=vulnerability_type
                        ):
                            vulnerability["request"] = self.escape_html(
                                vulnerability.get("request") or ""
                            )
                            vulnerability["location"] = location
                            template_vars["template_part"] = "affected_item"
                            template_vars["vulnerability"] = vulnerability
                            template_vars["vulnerability_type"] = vulnerability_type
                            template_vars["severity_string"] = self.get_severity_string(
                                vulnerability_type.get("severity")
                            )
                            file_affected_items.write(template.render(template_vars))

                    common_parts_template_vars["template_part"] = "display_scan_info"
                    common_parts_template_vars[
                        "threat_level"
                    ] = self.calculate_threat_level(alerts_count)
                    common_parts_template_vars[
                        "scan_info"
                    ] = self.get_well_formed_scan_info(source)
                    common_parts_template_vars["alerts_count"] = alerts_count
                    data.write(common_parts_template.render(common_parts_template_vars))
                    data.seek(0)
                    yield data
                    data = StringIO()
                    file_affected_items.seek(0)
                    yield file_affected_items
                    file_scanned_items.seek(0)
                    yield file_scanned_items

        data.write(common_parts_template.render({"template_part": "body_end"}))
        data.seek(0)
        yield data
